#ifndef db_SqliteAdapter_h
#define db_SqliteAdapter_h

#include "Adapter.h"

struct sqlite3;
struct sqlite3_stmt;

namespace serika
{
	namespace db
	{
		class SqliteAdapter : public Adapter
		{
		public:
			SqliteAdapter();
			virtual ~SqliteAdapter();

			virtual bool Create(const Any& initInfo);

			virtual void Destroy();

			virtual int ReadRecords(std::vector<FieldMap*>& resultSet,
				const std::string& tableName,
				const std::string& conditions,
				int offset,
				int limit,
				const std::string& select,
				const std::string& joins,
				const std::string& order,
				const std::string& group,
				const std::string& having);

			virtual int Count(const std::string& tableName, const std::string& column, const std::string& conditions, const std::string& joins);

			virtual bool CreateRecord(FieldMap* fields, const std::string& tableName, const std::string& primaryKey);
			virtual bool CreateRecords(std::vector<FieldMap*>& fieldMaps, const std::string& tableName, const std::string& primaryKey);

			virtual int UpdateAllRecords(const std::string& settings, const std::string& conditions, const std::string& tableName);

			virtual int DeleteRecord(const std::string& conditions, const std::string& tableName);

			virtual void BeginTransaction();
			virtual void CommitTransaction();
			virtual void RollbackTransaction();

			virtual std::string EscapeString(const std::string& text) { return text; }

			virtual std::string ConstructMultipleInsert(const std::vector<FieldMap*>& fieldMapds, const std::string& tableName);

		private:
			inline void ThrowRecordException(int code, const char* message);
			inline void ThrowRecordException(int code);

			inline sqlite3_stmt* Prepare(const std::string& sql);
			inline void Finalize(sqlite3_stmt* stmt);

			inline void Exec(const std::string& sql);

		private:
			sqlite3* mSqlite;
		};
	}
}

#endif
